# -*- coding: UTF-8 -*-
import requests
import json
from bs4 import BeautifulSoup
import pandas as pd

from data_interface import DataInterface

'''
定投基金收益数据，转换为DataFrame
定投收益排行地址：http://fund.eastmoney.com/api/Dtshph.ashx
返回值：jQuery18302547757122397052_1582185531332({"total":4248,"pageIndex":1……定投</a></td></tr></tbody></table>"})

# callback = 'jQuery18308791608443714658_1582188591207'
# _ = '1582188591226'
'''


class InterfaceFundIncome(object):

    def __init__(self, callback, parament):

        self.callback = callback
        self.url = 'http://fund.eastmoney.com/api/Dtshph.ashx'

        self.params = {
            "t": 2,  # 全部基金-0, 股票型-1, 混合型-2, 债券型-3, 指数型-4, QDII-5
            "c": "yndt",  # 按那列数值排序, dwjz-按单位净值, yndt-按一年定投
            "s": "desc",  # 排序, 降序-desc
            "issale": 1,  # 可购-1, 全部-0
            "page": 1,  # 当前展示页的页数
            "psize": 20,  # 分页-100, 不分页-20000
            "callback": callback,
            "_": parament
        }

    def getadata_fundincome(self):
        start_str = self.callback + '('  # 'jQuery18306380296455617336_1582286095728('
        end_str = ')'
        keyname = 'data'
        fi = DataInterface(self.url, self.params, start_str, end_str)
        str_data = fi.get_response(keyname)
        return self.parsexml_fundincome(str_data)

    def parsexml_fundincome(self,xmldata):

        soup = BeautifulSoup(str(xmldata), "lxml")
        # table = soup.table

        # 获取列名
        thead = soup.thead
        ths = thead.find_all('th')
        cols = []
        for th in ths:
            # print(th.get_text())
            if ths.index(th) == 2:
                cols.append(th.get_text())
            elif ths.index(th) == 3:
                cols.append(th.get_text())
            elif ths.index(th) == 5:
                cols.append(th.get_text())
            elif ths.index(th) == 7:
                cols.append(th.get_text())
            elif ths.index(th) == 8:
                cols.append(th.get_text())
            elif ths.index(th) == 9:
                cols.append(th.get_text())
            elif ths.index(th) == 11:
                cols.append(th.get_text())
            elif ths.index(th) == 12:
                cols.append(th.get_text())
        # 获取二维数据
        tbody = soup.tbody
        trs = tbody.find_all('tr')
        rows = []
        for tr in trs:
            tds = tr.find_all('td')
            row = []

            for td in tds:
                if tds.index(td) == 2:
                    row.append(td.get_text())
                elif tds.index(td) == 3:
                    row.append(td.get_text())
                elif tds.index(td) == 5:
                    row.append(float(td.get_text()))
                elif tds.index(td) == 7:
                    row.append(float(td.get_text().strip("%")))
                elif tds.index(td) == 8:
                    if td.get_text() != '--':
                        row.append(float(td.get_text().strip("%")))
                    else:
                        row.append(td.get_text())
                elif tds.index(td) == 9:
                    if td.get_text() != '--':
                        row.append(float(td.get_text().strip("%")))
                    else:
                        row.append(td.get_text())
                elif tds.index(td) == 11:
                    if td.get_text() == '暂无评级':
                        row.append(0)
                    else:
                        row.append(len(td.get_text()))
                elif tds.index(td) == 12:
                    row.append(float(td.get_text().strip("%")))

            if (row[4] != '--') & (row[5] != '--'):
                rows.append(row)
        return pd.DataFrame(rows, index=list(range(1, len(rows) + 1)), columns=cols)


    # def get_response(self):
    #
    #     # 返回的值不是json格式，无法直接使用r.json()方法转换返回值的格式
    #     jqdata = requests.get(self.url, params=self.params, headers=self.headers)
    #     return self.response2json(jqdata)
    #
    # def response2json(self, responsedata):
    #
    #     rem_str = self.callback + '(' # 'jQuery18306380296455617336_1582286095728('
    #     rem_end = ')'
    #     rs_text = responsedata.text
    #     rs_text = rs_text.lstrip(rem_str).rstrip(rem_end)
    #     return json.loads(rs_text)




if __name__ == '__main__':

    # date = ""
    dfi = InterfaceFundIncome('jQuery18307394209131639307_1582695425022', '1582695425036')
    json_data = dfi.get_response()
    # print('json_data["data"]: %s' % json_data['data'])
    soup = BeautifulSoup(str(json_data['data']), "lxml")
    # table = soup.table

    # 获取列名
    thead = soup.thead
    ths = thead.find_all('th')
    cols = []
    for th in ths:
        # print(th.get_text())
        if ths.index(th) == 2:
            cols.append(th.get_text())
        elif ths.index(th) == 3:
            cols.append(th.get_text())
        elif ths.index(th) == 5:
            cols.append(th.get_text())
        elif ths.index(th) == 7:
            cols.append(th.get_text())
        elif ths.index(th) == 8:
            cols.append(th.get_text())
        elif ths.index(th) == 9:
            cols.append(th.get_text())
        elif ths.index(th) == 11:
            cols.append(th.get_text())
        elif ths.index(th) == 12:
            cols.append(th.get_text())

    # print("cols is: ")
    # for col in cols:
    #     print(col)

    # 获取二维数据
    tbody = soup.tbody
    trs = tbody.find_all('tr')
    rows = []
    for tr in trs:
        tds = tr.find_all('td')
        row = []

        for td in tds:
            if tds.index(td) == 2:
                row.append(td.get_text())
            elif tds.index(td) == 3:
                row.append(td.get_text())
            elif tds.index(td) == 5:
                row.append(float(td.get_text()))
            elif tds.index(td) == 7:
                row.append(float(td.get_text().strip("%")))
            elif tds.index(td) == 8:
                if td.get_text() != '--':
                    row.append(float(td.get_text().strip("%")))
                else:
                    row.append(td.get_text())
            elif tds.index(td) == 9:
                if td.get_text() != '--':
                    row.append(float(td.get_text().strip("%")))
                else:
                    row.append(td.get_text())
            elif tds.index(td) == 11:
                if td.get_text() == '暂无评级':
                    row.append(0)
                else:
                    row.append(len(td.get_text()))
            elif tds.index(td) == 12:
                row.append(float(td.get_text().strip("%")))

        if (row[4] != '--') & (row[5] != '--'):
            rows.append(row)
    # len_rows = len(rows)
    # print('len is: %d' % len_rows)
    df = pd.DataFrame(rows, index=list(range(1, len(rows)+1)), columns=cols)
    # df = pd.DataFrame(rows, columns=cols)
    # df.index = df.index + 1
    df['按1年收益排序'] = df.index
    # print(df)
    end = int(round(df.shape[0] / 4))
    df1 = df.loc[1:end,'代码':'简称']

    print("\n --------------------------- 2排序之后 ---------------------------\n")
    # DataFrame.sort_values - 排序, 按照指定的列排序(by=cols[4])，降序排列(ascending=False)，排列后的数据替换当前的数据(inplace=True)
    df.sort_values(by=cols[4], inplace=True, ascending=False)
    df.reset_index(drop=True, inplace=True)     # 排列后的索引是乱序的，重置索引，从0开始
    df.index = df.index + 1
    # print('shape is: %d' % df.shape[0])
    # df.reindex(index=list(range(1, df.shape[0]+1)))
    df['按2年收益排序'] = df.index       # 提取行索引，并赋值给新添加的最后列‘按2年收益排序’
    # print(df)
    df2 = df.loc[1:end, '代码':'简称']

    print("\n --------------------------- 3排序之后 ---------------------------\n")
    df.sort_values(by=cols[5], inplace=True, ascending=False)  # 按照指定的列排序，降序排列，排列后的数据替换当前的数据
    df.reset_index(drop=True, inplace=True)  # 排列后的索引是乱序的，重置索引，从0开始
    df.index = df.index + 1
    df['按3年收益排序'] = df.index


    # DataFrame.eval-多个列计算, 参数inplace-是否在原数据上操作, False 将会生成新的DataFrame
    df.eval('合计排行 = 按1年收益排序+按2年收益排序+按3年收益排序', inplace=True)
    df.sort_values(by='合计排行', inplace=True, ascending=True)
    df.reset_index(drop=True, inplace=True)
    df.index = df.index + 1

    print("\n --------------------------- df 数据 ---------------------------\n")
    print(df)

    print("\n --------------------------- df3 数据 ---------------------------\n")
    df3 = df.loc[1:end, '代码':'简称']
    print(df3)

    print("\n --------------------------- df4 数据 ---------------------------\n")
    df4 = pd.merge(pd.merge(df1, df2, on=['代码', '简称']), df3, on=['代码', '简称'])
    print(df4)



